import axios from "axios";

// 请求拦截器 设置请求头  携带token
axios.interceptors.request.use(
  (config) => {
    const token = sessionStorage.getItem("token");
    if (token) {
      config.headers.Authorization = token;
    }

    // 设置请求头类型  // 只能解析application/x-www-form-urlencoded格式 // app.use(express.urlencoded({ extended: false }));
    config.headers["Content-Type"] = "application/x-www-form-urlencoded";
    config.headers["Accept"] = "application/json";

    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

// 相应拦截器
axios.interceptors.response.use((response) => {
  if (response.status === 200) {
    return response.data;
  } else {
    return Promise.reject(response.data);
  }
});

export default axios;
